package com.example.tangjiangxu.fragmentdemo2;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class TwoFragment extends Fragment {
    private static final String TAG = "TwoFragment";
    private OnClickTwoButtonListener onClickTwoButton;

    /**
     * 回调接口
     */
    public interface OnClickTwoButtonListener {
        void onClickTwoButton();
    }

    /**
     * 为Activity提供方法，设置OnClickToThreeButton对象
     * @param onClickTwoButton
     */
    public void setOnClickTwoButtonListener(OnClickTwoButtonListener onClickTwoButton) {
        this.onClickTwoButton  = onClickTwoButton;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.e(TAG, "onCreateView: ");
        View view = inflater.inflate(R.layout.fragment_two, container, false);
        Button bn = (Button) view.findViewById(R.id.bnt_three);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(TAG, "onClick: ");
                if (onClickTwoButton != null) {
                    Log.e(TAG, "onClickTwoButton != null: ");
                    onClickTwoButton.onClickTwoButton();
                }
            }
        });
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: ");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.e(TAG, "onDestroyView: ");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.e(TAG, "onDetach: ");
    }
}
